package org.puzzle2;

import java.awt.CheckboxMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.Effect;
import javax.media.Manager;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.UIManager;

import org.puzzle2.capture.CaptureVideoPanel;
import org.puzzle2.effect.TrackEffect;
import org.puzzle2.effect.TrackEffectPanel;

/**
 * <ul>
 * <li>Processeur simple jouant le role de player grace a
 * processor.setContentDescriptor(null)
 * <li>Ajout de l'effet PuzzleEffect
 * </ul>
 */

class PuzzleVideoUI extends JFrame {

	private PuzzleVideoPanel puzzleVideoPanel = null;

	private CaptureVideoPanel captureVideoPanel = null;

	private String mediaFile = null;

	JFrame me;

	/**
	 * Construction de l'interface
	 * 
	 * @param mediaFile
	 */
	public PuzzleVideoUI(String mediaFile) {
		me = this;
		this.mediaFile = mediaFile;

		try {
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch (Exception e) {
			System.err.println("Could not initialize java.awt Metal lnf");
		}

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				if (puzzleVideoPanel != null) {
					puzzleVideoPanel.close();
				}
				if (captureVideoPanel != null) {
					captureVideoPanel.close();
				}
				System.exit(0);
			}
		});
		// setLayout(new BorderLayout());
		setSize(600, 600);
		setJMenuBar(createMenuBar());
		JPanel panel = new JPanel();
		panel.setDoubleBuffered(true);
		setContentPane(panel);
		getContentPane().add("Center", new JPanel());

		setVisible(true);

	}

	public JMenuBar createMenuBar() {
		JMenuBar menuBar;
		JMenu menu, submenu;
		JMenuItem menuItem;
		JRadioButtonMenuItem rbMenuItem;
		JCheckBoxMenuItem cbMenuItem;

		// Create the menu bar.
		menuBar = new JMenuBar();

		// Build the first menu.
		menu = new JMenu("File Source");
		menuBar.add(menu);

		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (captureVideoPanel != null) {
					captureVideoPanel.close();
				}
				if (puzzleVideoPanel != null) {
					puzzleVideoPanel.close();
				}

				puzzleVideoPanel = new PuzzleVideoPanel(mediaFile, arg0
						.getActionCommand());
				// setContentPane(puzzleVideoPanel);
				getContentPane().add("Center", puzzleVideoPanel);
			}
		};

		// a group of JMenuItems
		menuItem = new JMenuItem("No Effect!!");
		menuItem.setActionCommand(null);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);

		menuItem = new JMenuItem("Puzzle Effect !!");
		menuItem.setActionCommand(Constantes.PUZZLE_EFFECT);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);

		menuItem = new JMenuItem("Mouse Location Effect !!");
		menuItem.setActionCommand(Constantes.MOUSE_LOCATION_EFFECT);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);

		menuItem = new JMenuItem("Track Effect");
		menuItem.setActionCommand(Constantes.TRACK_EFFECT);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);

		menuItem = new JMenuItem("Track Effect2");
		menuItem.setActionCommand(Constantes.TRACK_EFFECT2);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);
		menuItem = new JMenuItem("Track Effect Settings");
		actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Effect effect = puzzleVideoPanel.getAbstractEffect();
				JFrame dialog = new JFrame();
				TrackEffectPanel trackEffectPanel = new TrackEffectPanel();
				trackEffectPanel.setEffect(effect);
				dialog.getContentPane().add(trackEffectPanel);
				dialog.pack();
				dialog.setLocationRelativeTo(me);
				dialog.setVisible(true);
			}
			
			
		};
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);

		// Build the first menu.
		menu = new JMenu("WebCam Source");
		menuBar.add(menu);
		actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (puzzleVideoPanel != null) {
					puzzleVideoPanel.close();
				}
				if (captureVideoPanel == null) {
					captureVideoPanel = new CaptureVideoPanel(arg0
							.getActionCommand());
					getContentPane().add("Center", captureVideoPanel);
				}
			}
		};

		menuItem = new JMenuItem("No Effect!!");
		menu.add(menuItem);

		menuItem = new JMenuItem("Puzzle Effect");
		menuItem.setActionCommand(Constantes.PUZZLE_EFFECT);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);

		menuItem = new JMenuItem("Mouse Location Effect");
		menuItem.setActionCommand(Constantes.MOUSE_LOCATION_EFFECT);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);

		menuItem = new JMenuItem("Track Effect");
		menuItem.setActionCommand(Constantes.TRACK_EFFECT);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);
		
		menuItem = new JMenuItem("Track Effect2");
		menuItem.setActionCommand(Constantes.TRACK_EFFECT2);
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);
		menuItem = new JMenuItem("Track Effect Settings");
		actionListener = new ActionListener() {
//			public void actionPerformed(ActionEvent arg0) {
//				Effect effect = captureVideoPanel.getAbstractEffect();
//
//				JDialog dialog = new JDialog(me);
//				TrackEffectPanel trackEffectPanel = new TrackEffectPanel();
//				trackEffectPanel.setEffect(effect);
//				dialog.getContentPane().add(trackEffectPanel);
//				dialog.pack();
//				dialog.setLocationRelativeTo(me);
//				dialog.setVisible(true);
//			}
			
			public void actionPerformed(ActionEvent arg0) {
				Effect effect = captureVideoPanel.getAbstractEffect();
				JFrame dialog = new JFrame();
				TrackEffectPanel trackEffectPanel = new TrackEffectPanel();
				trackEffectPanel.setEffect(effect);
				dialog.getContentPane().add(trackEffectPanel);
				dialog.pack();
				dialog.setLocationRelativeTo(me);
				dialog.setVisible(true);
			}
		};
		menuItem.addActionListener(actionListener);
		menu.add(menuItem);
		/**
		 * REORGANISATION DU MENU. POUR LA SUITE
		 */

		menu = new JMenu("Source");
		menuBar.add(menu);
		menuItem = new JMenuItem("Fichier");
		menu.add(menuItem);
		menuItem = new JMenuItem("WebCam");
		menu.add(menuItem);

		menu = new JMenu("Filtre");
		menuBar.add(menu);
		menuItem = new JMenuItem("Puzzle");
		menu.add(menuItem);
		menuItem = new JMenuItem("Click Sourie");
		menu.add(menuItem);
		menuItem = new JMenuItem("Traque");
		menu.add(menuItem);

		menu = new JMenu("Processeur");
		menuBar.add(menu);
		JCheckBoxMenuItem menuItemCB = new JCheckBoxMenuItem("Boucle");
		menuItemCB.setState(true);
		menu.add(menuItemCB);

		menu = new JMenu("Sortie");
		menuBar.add(menu);
		menuItem = new JMenuItem("Fichier");
		menu.add(menuItem);

		return menuBar;
	}

	public static void main(String[] args) {
		PuzzleVideoUI player1 = new PuzzleVideoUI("file:.\\balle-orange.avi");
		// PuzzleVideoUI player1 = new PuzzleVideoUI(args[0]);
	}

}